var util = require('../../../utils/util.js');
var apiObject = require('../../../utils/api.js');
var app = getApp();

Page({

  data: {
    elementStatus:{
      settingValue:0,
      minLimit:0,
      maxLimit:100,
      monitorMin: 0,
      monitorMax: 100,
    },
    flag:false
  },

  onLoad:function(e){
    this.setData({
      id:e.id
    })
  },
  onShow: function (e) {
    var that=this;
    var id=this.data.id;
    util.request(apiObject.api.getElementStatusById, {
      id: id
    }).then(function (res) {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          elementStatus: res.result.elementStatus,
          flag:true
        })
        
      } else {
        util.showModal(res.message);
      }
    })
  },

  minChange: function (e) {
    var target = "elementStatus.monitorMin"
    this.setData({
      [target]: e.detail.lowValue
    })
  },

  maxChange: function (e) {
    var target = "elementStatus.monitorMax"
    this.setData({
      [target]: e.detail.heighValue
    })
  },

  valueChange: function (e) {
    var target = "elementStatus.settingValue"
    this.setData({
      [target]: e.detail.heighValue
    })
  },
  submit: function () {
    var that =this;
    console.log(that.data.elementStatus);
    util.request(apiObject.api.saveElementStatus, {
      id: that.data.elementStatus.id,
      monitorMax: that.data.elementStatus.monitorMax,
      monitorMin: that.data.elementStatus.monitorMin,
      settingValue: that.data.elementStatus.settingValue,
    }).then(function (res) {
      console.log(res)
      if (res.code == 200) {
        util.showModal("保存成功");
      } else {
        util.showModal(res.message);
      }
    })
  }
})